<?php
/**
 * Html code of user editing page (the one with forms).
 * 
 * @package templates
 */

	$cm = CodeManager::getCodeManager();

	if (!Policy::isAdminView())  {
		Error::getError()->addErrorMessage($cm->get("10005"));
		return;
	}
?>

<div id="offer-edit">
<?php
	$formActionString = "/index.php?";
	$formActionString .= Constants::REQ_ACTION."=".Constants::REQ_ACTION_REDIRECT;
	$formActionString .= "&amp;";
	$formActionString .= Constants::REQ_TYPE."=".Constants::REQ_TYPE_REGISTRATION;
	
	$cm = CodeManager::getCodeManager();
	
	
	
	
	$dbConnector = SiteContext::getSiteContext()->getDBConnector();
	
	$isedit = false;
	
	if(isset($_GET[Constants::REQ_TYPE]) && $_GET[Constants::REQ_TYPE] != Constants::REQ_TYPE_REGISTRATION) {
		$id = (int)mysql_real_escape_string($_GET[Constants::REQ_TYPE], DBConnectorMySQL::getInitialResource());
		$res = $dbConnector->query("select * from users where id = $id limit 1"); // Make constant from this query
		
		$arr = mysql_fetch_assoc($res); // TODO what if this throws an exception?
		HtmlHelper::setValuesToInit($arr);
		$isedit = true;
	} else {
		HtmlHelper::setValuesToInit($_POST);
		$id = (int)@$_POST['id'];
	}
?>
<form id="registration" action="<?php echo $formActionString; ?>" method="post" enctype="multipart/form-data">
	<table>
		<tbody>
			<tr>
				<td class="left">
					<?php echo $cm->get("30001"); ?>:
				</td>
				<td class="right">
					<input <?php echo HtmlHelper::inputValueWithAttr("login"); if ($isedit) echo ' disabled="disabled"'; ?>/>
					<?php if ($isedit) { ?>
						<input class="invisible" <?php echo HtmlHelper::inputValueWithAttr("login"); ?>/>
					<?php } ?>
				</td>
			</tr>
			<tr>
				<td class="left">
					<?php echo $cm->get("30002"); ?>:
				</td>
				<td class="right">
					<input type="password" name="password" />
				</td>
			</tr>
			<tr>
				<td class="left">
					<?php echo $cm->get("30005"); ?>:
				</td>
				<td class="right">
					<input type="password" name="passwordc" />
				</td>
			</tr>
			<tr>
				<td class="left">
					<?php echo $cm->get("30006"); ?>:
				</td>
				<td class="right">
					<input <?php echo HtmlHelper::inputValueWithAttr("mail"); ?>/>
				</td>
			</tr>
			<tr>
				<td class="left">
					<?php echo $cm->get("30028"); ?>:
				</td>
				<td class="right">
					<input <?php echo HtmlHelper::inputValueWithAttr("phone"); ?>/>
				</td>
			</tr>
			<tr>
				<td class="left">
					<?php echo $cm->get("30007"); ?>:
				</td>
				<td class="right">
					<input <?php echo HtmlHelper::inputValueWithAttr("firstName"); ?>/>
				</td>
			</tr>
			<tr>
				<td class="left">
					<?php echo $cm->get("30008"); ?>:
				</td>
				<td class="right">
					<input <?php echo HtmlHelper::inputValueWithAttr("surName"); ?>/>
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30033"); ?>: </td>
				<td class="right">
					<select class="large" name="group">
						<?php 
							foreach (Definitions::$SETUP_GROUPS as $groupId => $groupName) {
								echo "<option " . HtmlHelper::selectedSelectWithAttr("group", $groupId) . "> $groupName</option>";
							}
						?>
					</select>
				</td>
			</tr>
<?php 
try {
	$res = $dbConnector->query("select * from users where id = $id limit 1"); // TODO Make constant from this query
	if ($htmlUser = HtmlUserAdmin::fetchInstance($res)) {
?>
			<tr>
				<td class="left vertical-spacer">
					<?php echo $cm->get("30021"); ?>:
				</td>
				<td class="vertical-spacer">
					(<?php echo $cm->get("30029"); ?>.)
				</td>
			</tr>
			<tr>
				<td style="width: 500px" colspan="2">
<?php 


		function processImage($object, $fileName) {
			$attrValue = Common::escapeThumbImgToAttr($fileName);
			$object->replaceTemplate("/_TEMPLATE_ADDED_CHECKBOX_/", HtmlHelper::selectedInputCheckboxWithAttr($attrValue));
		}
		
		
		
		$htmlCode = '
						<div class="thumbwrapped framed">
							<div class="swc0"><div class="swc1">
								<div class="swc2"> 
										<img src="_TEMPLATE_THUMBSRC_" alt="" />
								</div>
								<span class="swc3"/>
							</div></div>
						</div>
						<div class="checkbox-delete framed">
							<input class="checkbox" type="checkbox" _TEMPLATE_ADDED_CHECKBOX_ />
						</div>';
		$htmlUser->processImages($htmlCode, 'processImage');
		$htmlUser->flushMe();
?>
				</td>
			</tr>
<?php 
	}
} catch(ExceptionQueryFailed $e) {
}
?>
			<tr id="offer-edit-empty-one-image-input">
				<td class="left">
					<?php echo $cm->get("30022"); ?>:
				</td>
				<td class="right">
					<!-- onblur="Common.conditionallyDeleteImgInput(this); return false;"  -->
					<input type="file" name="img1" />
				</td>
			</tr>
		</tbody>
<?php 
try {
	$res = $dbConnector->query("select * from offers where agentId = $id "); // Make constant from this query
	$shouldPrintBeggining = true;
	$shouldPrintEnding = false;
	while ($offer = Offer::mysqlFetch($res)) {
		if ($shouldPrintBeggining) {
			$shouldPrintEnding = true;
?>
		<tbody>
			<tr>
				<td class="left vertical-spacer">
					<?php echo $cm->get("30032"); ?>
				</td>
				<td class="vertical-spacer">(<?php echo $cm->get("30031", array(SiteContext::getSiteContext()->getCurrentUser()->login)); ?>)</td>
			</tr>
<?php 
		}
		$shouldPrintBeggining = false;
	
		echo '<tr><td class="left"><input class="checkbox" type="checkbox" '
			. HtmlHelper::selectedInputCheckboxWithAttr("offer_".$offer->id)
			. '/></td><td><a href="'
			. HtmlOfferBase::hrefToOffer($offer->id)
			. '">'
			. $offer->locality ." | ". $offer->town ." | ". $offer->street
			. '</a></td></tr>';
	}

	if ($shouldPrintEnding) {
?>
				
		</tbody>
<?php 
	}
} catch (ExceptionQueryFailed $e) {
}
?>
		<tbody>
			<tr>
				<td  colspan="2" class="submit">
<?php 
	$info = SiteContext::getSiteContext();
	$random = Common::createFormHash();
	$info->setFormHash($random);
?>
					<input type="text" value="<?php echo $random; ?>" class="invisible" name="formHash" />
					
					
					<input type="text" class="invisible" <?php echo HtmlHelper::inputValueWithAttr("id"); ?>/>
					<input class="submit middle" type="submit" value="<?php echo $cm->get("20005"); ?>" />
				</td>
			</tr>
		</tbody>
	</table>
	
</form>
</div>